import Vue from 'vue'
import VueRouter from 'vue-router'
import { Toast } from "vant";

Vue.use(VueRouter)

const router = new VueRouter({
    routes: [{
            path: '/',
            redirect: "/home"
        },

        //  首页
        {
            name: "Home",
            path: "/home",
            component: () => import("../pages/home"),
            meta: {
                showFooter: true,
                title: "首页",
                baseShowHeader: false,
                keepList: false
            }
        },

        // 博客 路由重定向
        {
            path: '/play',
            redirect: "/play/recommend"
        },
        //  play 博客 
        {
            name: "play",
            path: "/play",
            component: () => import("../pages/play"),
            meta: {
                showFooter: true,
                title: "博客",
                baseShowHeader: false,
            },
            children: [{
                    //听听
                    path: 'listen',
                    component: () => import('@/pages/play/listen/index.vue'),
                    meta: {
                        title: '听听',
                        showFooter: true,
                    },
                },
                {
                    // 推荐
                    path: 'recommend',
                    component: () => import('@/pages/play/recommend/index.vue'),
                    meta: {
                        title: '推荐',
                        showFooter: true,
                    },
                },
                {
                    //分类
                    path: 'classify',
                    component: () => import('@/pages/play/classify/index.vue'),
                    meta: {
                        title: '分类',
                        showFooter: true,
                    },
                },
            ],
        },
        // {
        //     name: "listen",
        //     path: "/play/listen",
        //     component: () => import("../pages/play/listen"),
        //     meta: {
        //         showFooter: true,
        //         title:"播客",
        //         baseShowHeader:false,
        //     }
        // },

        // 博客  ldp分类详情
        {
            path: '/classifydetail/:id',
            component: () =>
                import('../pages/play/classify/classifydetail/index.vue'),
            meta: {
                title: '',
                baseShowHeader: true,

            },
        },
        //  关注 路由重定向
        {
            path: '/focus',
            redirect: "/focus/dynamic"
        },
        // focus 关注
        {
            name: "focus",
            path: "/focus",
            component: () => import("../pages/focus"),
            meta: {
                showFooter: true,
                title: "关注",
                baseShowHeader: false,
            },
            children: [
                // 云圈
                {
                    path: 'cloud',
                    component: () => import('@/pages/focus/cloud'),
                    meta: {
                        title: '云圈',
                        showFooter: true,
                    },
                },
                //动态
                {
                    path: 'dynamic',
                    component: () => import('@/pages/focus/dynamic/index.vue'),
                    meta: {
                        title: '动态',
                        showFooter: true,
                    },
                },

            ]
        },
        // topic 话题详情
        {
            path: '/topic/:id',
            component: () => import('../pages/focus/dynamic/topicitem/index.vue'),
            meta: {
                title: '话题详情',
                baseShowHeader: true,
            },
        },
        //  我的
        {
            name: "Mine",
            path: "/mine",
            component: () => import("../pages/mine"),
            meta: {
                showFooter: true,
                title: "我的",
                baseShowHeader: false,
            }
        },
        //  登录界面
        {
            name: "Login",
            path: "/login",
            component: () => import("../pages/login"),
            meta: {
                title: "登录",
                baseShowHeader: true,
            },
        },

        //home页面下的四个子页面
        //rank 排行榜
        {
            path: '/rank',
            name: 'rank',
            component: () => import('../pages/rank/index.vue'),
            meta: {
                title: '排行榜',
                baseShowHeader: true,
            },
        },
        {
            path: '/ranklist',
            name: 'ranklist',
            component: () => import('../pages/ranklist/index.vue'),
            meta: {
                title: '排行榜歌单详情',
                baseShowHeader: true,
            },
        },
        //songsheet 歌单
        {
            path: '/songsheet',
            name: 'songsheet',
            component: () => import('../pages/songsheet/index.vue'),
            meta: {
                title: '歌单',
                baseShowHeader: true,
            },
        },
        //daily  每日推荐
        {
            path: '/daily',
            name: 'daily',
            component: () => import('../pages/daily/index.vue'),
            meta: {
                title: '每日推荐',
                baseShowHeader: true,
            },
        },
        //fm  私人FM
        {
            path: '/fm',
            name: 'fm',
            component: () => import('../pages/fm/index.vue'),
            meta: {
                title: '私人FM',
                baseShowHeader: true,
            },
        },
        // 个人资料
        {
            path: '/personal',
            component: () => import('../pages/personal/index.vue'),
            meta: {
                title: '个人资料',
                baseShowHeader: true,
            },
        },
        //喜欢列表(mine)
        {
            path: '/mylove/:id',
            name: 'mylove',
            component: () => import('../pages/mylove'),
            meta: {
                title: '我的',
                baseShowHeader: true,
            },
        },
        // 歌单详情
        {
            path: '/songlistdetail/:id',
            name: 'Songlistdetail',
            component: () => import('../pages/songlistdetail/index.vue'),
            meta: {
                title: '歌单详情',
                baseShowHeader: true,
            },
        },
        //歌曲播放
        {
            path: '/songdetail/:id',
            name: 'Songdetail',
            component: () => import('../pages/songdetail/index.vue'),
            meta: {
                title: '播放歌曲',
                baseShowHeader: true,
            },
        },

        //mine的子路由
        //创建歌单
        {
            path: '/creatlist/:id',
            name: 'creatlist',
            component: () => import('../pages/mine/musiclist/creatlist.vue'),
            meta: {
                title: '创建歌单',
                baseShowHeader: true,
            },
        },
        //收藏歌单
        {
            path: '/liststar/:id',
            name: 'liststar',
            component: () => import('../pages/mine/musiclist/liststar.vue'),
            meta: {
                title: '收藏歌单',
                baseShowHeader: true,
            },
        },
        //歌单助手
        {
            path: '/listhelper/:id',
            name: 'listhelper',
            component: () => import('../pages/mine/musiclist/listhelper.vue'),
            meta: {
                title: '歌单助手',
                baseShowHeader: true,
            },
        },
        {
            // 搜索
            path: '/search',
            component: () => import('../pages/search/index.vue'),
            meta: {
                title: '搜索',
                baseShowHeader: true,
            },
          },
    ],
})
router.beforeEach((to,from,next) => {
    // console.log('afterEach',to,from)
    if (to.path == '/login') {
        next()
    } else {
        if (!localStorage.getItem('uid')) {
            Toast.fail('请先登录');
            // console.log(getToken('MUSIC_U'));
            next('/login')
        }
        else{
            next()
        }
    }
})
router.afterEach((to) => {
    // console.log('afterEach',to,from)
    if (to.meta.title) {
        document.title = to.meta.title //修改网页的title
    } else {
        document.title = 'Five'
    }
})

export default router